package com.example.sample_for_work;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.sample_for_work_library.Dir_file;
import com.example.sample_for_work_library.ViewUtils;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

/**
 * @author 說明:
 * ViewPager控件使用的Demo.
 */
public class ViewPagerActivity extends Activity {

	private static final int ALBUM_COUNT = 100;

	private CharSequence[] pic_list;
	
	private ViewPager mViewPager;
	private ViewPagerAdapter mViewPagerAdapter;
	private JSONArray mJsonArray;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_activity);
        pic_list = Dir_file.img_file();
        setupViews();
        
        ImageButton menubtn = (ImageButton)findViewById(R.id.menubtn);
        menubtn.setMinimumHeight(100);
        menubtn.setMinimumWidth(100);
        menubtn.setOnClickListener(click);
    }
    
    public void pop(View v){
    	ViewUtils.initpop(this,v);
    }
    
    private OnClickListener click=new OnClickListener(){
		@Override
		public void onClick(View v) {
			pop(v);
		}
 
    };
    
    private void setupViews(){  
    	//提供數據
    	mJsonArray = new JSONArray();
    	for(int i = 0;i<ALBUM_COUNT; i++){
    		JSONObject object = new JSONObject();
    		try {
    			//圖片循環
    			object.put("resid", pic_list[i % pic_list.length].hashCode());
				object.put("name", pic_list[i % pic_list.length]);
	    		mJsonArray.put(object);
			} catch (JSONException e) {
				e.printStackTrace();
			}
    		
    	}    	
    	mViewPager = (ViewPager)findViewById(R.id.viewpager);
    	mViewPagerAdapter = new ViewPagerAdapter(this, mJsonArray);
    	mViewPager.setAdapter(mViewPagerAdapter);
    }
}